﻿using Chloe;
using Chloe.SqlServer;
using KPI.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using TYBase;

namespace ScheduleManager.DB
{
    public class ScheduleDBHelper
    {
        public static RPage<ScheduleVO> GetList(PageInfo page, ScheduleVO queryDto)
        {
            try
            {
                var date = queryDto.schedule.start_date.Value.Date;
                using (MsSqlContext dx = DBHelper.CreateWMSConnection())
                {
                    var query = dx.Query<Public_sysuser>()
                             .LeftJoin<Tbl_Schedule>((mast, delt) => mast.s_ic_no == delt.user_id && delt.start_date == date);
                    var qq = query.Select((mast, delt) => new ScheduleVO
                    {
                        schedule = delt,
                        user_name = mast.s_name,
                        user_id = mast.s_ic_no,
                    });
                    //if (queryDto.schedule != null)
                    //{
                    //    if (queryDto.schedule.start_date != null)
                    //    {
                    //        qq = qq.Where((m, a) => a.start_date == queryDto.schedule.start_date);
                    //    }

                    //    //if (queryDto.end_date != null)
                    //    //{
                    //    //    query = query.Where((m, a) => a.end_date <= queryDto.end_date);
                    //    //}
                    //}

                    if (queryDto.user_name != null)
                    {
                        qq = qq.Where(x => x.user_name.Contains(queryDto.user_name));
                    }
                    int count = qq.Count();
                    var listData =
                        qq.Skip(page.PageSize * (page.PageIndex - 1)).Take(page.PageSize).ToList();
                    var r = RPage<ScheduleVO>.OK(listData, page);
                    r.SetDataCount(count);
                    return r;
                }
            }
            catch (Exception ex)
            {
                return RPage<ScheduleVO>.Fail(ex.Info());
            }
        }

        internal static R<bool> Edit(Tbl_Schedule schedule)
        {
            try
            {
                using (MsSqlContext dx = DBHelper.CreateWMSConnection())
                {
                    if (schedule.schedule_id > 0)
                    {
                        var data = dx.Query<Tbl_Schedule>().FirstOrDefault(x => x.schedule_id == schedule.schedule_id);
                        if (data == null)
                        {
                            return R<bool>.Fail($"未找到对应数据 schedule_id:{schedule.schedule_id}");
                        }
                        data.shift1_key = schedule.shift1_key;
                        data.shift2_key = schedule.shift2_key;
                        data.shift3_key = schedule.shift3_key;
                        data.shift4_key = schedule.shift4_key;
                        data.shift5_key = schedule.shift5_key;
                        data.shift6_key = schedule.shift6_key;
                        data.shift7_key = schedule.shift7_key;
                        data.modify_time = DateTime.Now;
                        dx.Update(data);
                    }
                    else
                    {
                        schedule.schedule_id = 0;
                        dx.Insert(schedule);
                    }
                    return R<bool>.OK();
                }
            }
            catch (Exception ex)
            {
                return R<bool>.Fail(ex.Info());
            }
        }
    }
}
